<?php
/**********************************************************
** Lime Manager
** Thomas Knobloch & Clément Troesch
** Users Ajax View
***********************************************************/
?>

<?php include_once "../../model/AdministratorModel.php"; ?>
<?php include_once "../../model/SessionModel.php"; ?>
<?php session_start(); ?>

<script type='text/javascript'>

function getXhr(){
	var xhr = null; 
	if(window.XMLHttpRequest) // Firefox et autres
	xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else { // XMLHttpRequest non supporté par le navigateur 
		alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		xhr = false; 
	} 
		
		return xhr;
}
			
function requestUser()
{

	var xhr = getXhr();
	
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4 && xhr.status == 200){
			leselect = xhr.responseText;
			document.getElementById('userRes').innerHTML = leselect;
		}
	}
	
	var user = document.getElementById('user').value;		

	xhr.open("POST","view/administrator/AdministratorUsersQueryView.php",true);
	xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	xhr.send("user="+user);
}
</script>

<?php
if($_SESSION['session']->currentConnection->type == "MYSQL"){
$owner="schema";
?>
<div class="section_title" id="utilisateurs"><h1> Objets - Schemas </h1></div>
<div class="section_info">
	Obtention des renseignements sur les schemas et leurs caractéristiques ainsi que pour chaque schema le nombre d'objets, par type, qu'il possède.
</div>
<?php
}
else if($_SESSION['session']->currentConnection->type == "ORACLE"){
$owner="utilisateur";
?>
<div class="section_title" id="utilisateurs"><h1> Utilisateurs </h1></div>
<div class="section_info">
	Obtention des renseignements sur les utilisateurs et leurs caractéristiques (login, profil, rôle, ...) ainsi que pour chaque utilisateur le nombre d'objets, par type, qu'il possède.
</div>
<?php
}
?>
			
<div class="article">

<div class="section_title_small_blue"><h4>Sélectionnez un <?php echo $owner ?></h4></div>

<?php
if(isset($_SESSION['session']->userLogin)){

?>
	<div class="tableContainer">
		<table class="tableQueryCenter">
			<tr>
			<th>AFFICHER</th>
			<?php
			if($_SESSION['session']->currentConnection->type == "MYSQL"){
			?>
				<th>Nom du schema</th>
			<?php
			}
			else if($_SESSION['session']->currentConnection->type == "ORACLE"){
			?>				
				<th>USERNAME</th>
				<th>USER_ID</th>
				<th>DEFAULT_TABLESPACE</th>
				<th>TEMPORARY_TABLESPACE</th>
				<th>ACCOUNT_STATUS</th>
				<th>PROFILE</th>
				<th>GRANTEE</th>
				<th>CREATED</th>
			<?php
			}
			?>
			</tr>
		<?php

		$nbrow = AdministratorModel::getUsersList($_SESSION['session']->currentConnection, $result);
		
		if(isset($_GET['selected_user']))
			$selectedUser = $_GET['selected_user'];
	
		foreach ($result as $row) {
		echo "<tr>";
		if($_SESSION['session']->currentConnection->type == "MYSQL"){
		
			if(isset($selectedUser) && $selectedUser == $row['SCHEMA_NAME'])
			{
				?><td><img src="img/check.png" alt="check" /></td><?php
			}
			else
			{
					?><td><a href="#objects"><img src="img/find.png" alt="zoom" onClick="displayUser('<?php echo $row['SCHEMA_NAME'] ?>')" /></a></td><?php
			}
			
			echo "<td>".$row['SCHEMA_NAME']."</td>\n";
		}
		else if($_SESSION['session']->currentConnection->type == "ORACLE"){
			
			if(isset($selectedUser) && $selectedUser == $row['USERNAME'])
			{
				?><td><img src="img/check.png" alt="check" /></td><?php
			}
			else
			{
					?><td><a href="#objects"><img src="img/find.png" alt="zoom" onClick="displayUser('<?php echo $row['USERNAME'] ?>')" /></a></td><?php
			}
			
			echo "<td>".$row['USERNAME']."</td>\n";
			echo "<td>".$row['USER_ID']."</td>\n";
			echo "<td>".$row['DEFAULT_TABLESPACE']."</td>\n";
			echo "<td>".$row['TEMPORARY_TABLESPACE']."</td>\n";
			echo "<td>".$row['ACCOUNT_STATUS']."</td>\n";
			echo "<td>".$row['PROFILE']."</td>\n";
			echo "<td>".$row['GRANTEE']."</td>\n";
			echo "<td>".$row['CREATED']."</td>\n";
		}
		echo"</tr>";
		}
	}
				
		?>
		</table>
	</div>
</div>

<?php
if(isset($selectedUser))
{
?>

<div class="section_title_small_green"><h4><?php echo $selectedUser ?></h4></div>

<div class="section_title_small_blue"><h4>Objets de <?php echo $selectedUser ?></h4></div>

<table class="tableQueryCenter">
			<tr>
			<?php
			if($_SESSION['session']->currentConnection->type == "MYSQL"){
			?>
				<th>Type d'objet</th>
				<th>Nombre</th>
			<?php
			}
			else if($_SESSION['session']->currentConnection->type == "ORACLE"){
			?>		
				<th>Type d'objet</th>
				<th>Nombre</th>
			<?php
			}
			?>
			</tr>
		<?php
			$nbrow =  AdministratorModel::getUserObjects($_SESSION['session']->currentConnection, $result, $selectedUser);
				foreach ($result as $row) {
				echo "<tr>";
				if($_SESSION['session']->currentConnection->type == "MYSQL"){
					echo "<td>".$row['OBJECT_TYPE']."</td>\n";
					echo "<td>".$row['COUNT(OBJECT_TYPE)']."</td>\n";
				}
				else if($_SESSION['session']->currentConnection->type == "ORACLE"){
					echo "<td>".$row['OBJECT_TYPE']."</td>\n";
					echo "<td>".$row['TOTAL']."</td>\n";
				}
				echo"</tr>";
				}
}
?>
</table>
